package org.jspresso.framework.application.frontend.action;

import java.util.List;
import java.util.Map;

import org.jspresso.framework.action.ActionBusinessException;
import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.application.backend.persistence.hibernate.HibernateBackendController;
import org.jspresso.framework.model.entity.IEntity;
import org.jspresso.tester.utils.TestEntitySerializer;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

public class StoreEntityAsTestEntity<E,F,G> extends FrontendAction<E,F,G> {
	@Override
	public boolean execute(final IActionHandler actionHandler, final Map<String, Object> context) {
		final List<IEntity> selectedEntities = getSelectedModels(context);
		getBackendController(context).getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// TODO Auto-generated method stub
				for(IEntity entity : getBackendController(context).cloneInUnitOfWork(selectedEntities)) {
					if(!entity.isPersistent()) {
						throw new ActionBusinessException("Entity isn't persisted !");
					}
					TestEntitySerializer.serializeToStore(entity, null, false, false, (HibernateBackendController) getBackendController(context));
				}
			}
		});
		
		return super.execute(actionHandler, context);
	}
}
